package com.irdstudio.bsp.executor.core.dao;

import com.irdstudio.bsp.executor.core.dao.domain.MigrateRuleResult;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 迁移校验规则校验结果类-[表名: 迁移校验规则校验结果(migrate_rule_result)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-13
 */
public class MigrateRuleResultDao {

	/* 连接对象 */
	Connection conn = null;	

	public MigrateRuleResultDao(Connection conn){
		this.conn = conn;
	}


    /**
	* 新增一行
	* @return int
	*/
    public int insertMigrateRuleResult(MigrateRuleResult migrateRuleResult) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("insert into migrate_rule_result ( "
                + "check_result_id,check_rule_id,subs_code,check_rule_name,table_id,table_code,table_name,check_rule_sql,check_expect_value,check_result_value,check_expect_flag"
                + ") values ("+ "?,?,?,?,?,?,?,?,?,?,?" +")"
                );
            ps.setObject(1, migrateRuleResult.getCheckResultId());
            ps.setObject(2, migrateRuleResult.getCheckRuleId());
            ps.setObject(3, migrateRuleResult.getSubsCode());
            ps.setObject(4, migrateRuleResult.getCheckRuleName());
            ps.setObject(5, migrateRuleResult.getTableId());
            ps.setObject(6, migrateRuleResult.getTableCode());
            ps.setObject(7, migrateRuleResult.getTableName());
            ps.setObject(8, migrateRuleResult.getCheckRuleSql());
            ps.setObject(9, migrateRuleResult.getCheckExpectValue());
            ps.setObject(10, migrateRuleResult.getCheckResultValue());
            ps.setObject(11, migrateRuleResult.getCheckExpectFlag());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("insert MigrateRuleResult is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键删除一行记录
	* @return int
	*/
    public int deleteByPk(MigrateRuleResult migrateRuleResult) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("delete from migrate_rule_result where 1=1"
                + " AND check_result_id = ? "
                );
            ps.setObject(1, migrateRuleResult.getCheckResultId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("delete MigrateRuleResult is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键更新一行记录
	* @return int
	*/
    public int updateByPk(MigrateRuleResult migrateRuleResult) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("update migrate_rule_result set "
                + " check_result_id = ? ,"
                + " check_rule_id = ? ,"
                + " subs_code = ? ,"
                + " check_rule_name = ? ,"
                + " table_id = ? ,"
                + " table_code = ? ,"
                + " table_name = ? ,"
                + " check_rule_sql = ? ,"
                + " check_expect_value = ? ,"
                + " check_result_value = ? ,"
                + " check_expect_flag = ? "
                + " where 1=1"
                + " AND check_result_id = ? "
                );
            ps.setObject(1, migrateRuleResult.getCheckResultId());
            ps.setObject(2, migrateRuleResult.getCheckRuleId());
            ps.setObject(3, migrateRuleResult.getSubsCode());
            ps.setObject(4, migrateRuleResult.getCheckRuleName());
            ps.setObject(5, migrateRuleResult.getTableId());
            ps.setObject(6, migrateRuleResult.getTableCode());
            ps.setObject(7, migrateRuleResult.getTableName());
            ps.setObject(8, migrateRuleResult.getCheckRuleSql());
            ps.setObject(9, migrateRuleResult.getCheckExpectValue());
            ps.setObject(10, migrateRuleResult.getCheckResultValue());
            ps.setObject(11, migrateRuleResult.getCheckExpectFlag());
            ps.setObject(12, migrateRuleResult.getCheckResultId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("update MigrateRuleResult is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }

	}

    /**
	* 根据主键查询一行记录
	* @return int
	*/
    public MigrateRuleResult queryByPk(MigrateRuleResult migrateRuleResult) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        MigrateRuleResult result = null;
        try {
            ps = conn.prepareStatement("select check_result_id,check_rule_id,subs_code,check_rule_name,table_id,table_code,table_name,check_rule_sql,check_expect_value,check_result_value,check_expect_flagfrom migrate_rule_result where 1=1 "
                + " AND check_result_id = ? "
                );
            ps.setObject(1, migrateRuleResult.getCheckResultId());
            rs = ps.executeQuery();
            if (rs.next()) {
				result = new MigrateRuleResult();
				result.setCheckResultId(rs.getString("check_result_id"));
				result.setCheckRuleId(rs.getString("check_rule_id"));
				result.setSubsCode(rs.getString("subs_code"));
				result.setCheckRuleName(rs.getString("check_rule_name"));
				result.setTableId(rs.getString("table_id"));
				result.setTableCode(rs.getString("table_code"));
				result.setTableName(rs.getString("table_name"));
				result.setCheckRuleSql(rs.getString("check_rule_sql"));
				result.setCheckExpectValue(rs.getBigDecimal("check_expect_value"));
				result.setCheckResultValue(rs.getBigDecimal("check_result_value"));
				result.setCheckExpectFlag(rs.getString("check_expect_flag"));
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateRuleResult is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	* 根据 migrateRuleResult 参数列表查询 不分页
	* @return int
	*/
    public List<MigrateRuleResult> queryAll(MigrateRuleResult migrateRuleResult) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<MigrateRuleResult> result = new ArrayList<>();
        try {
            String sql = "select check_result_id,check_rule_id,subs_code,check_rule_name,table_id,table_code,table_name,check_rule_sql,check_expect_value,check_result_value,check_expect_flagfrom migrate_rule_result where 1=1 ";
                if (migrateRuleResult.getCheckResultId() != null) {
                    sql = sql + " AND check_result_id =  '" + migrateRuleResult.getCheckResultId() + "'";
                }
                if (migrateRuleResult.getCheckRuleId() != null) {
                    sql = sql + " AND check_rule_id =  '" + migrateRuleResult.getCheckRuleId() + "'";
                }
                if (migrateRuleResult.getSubsCode() != null) {
                    sql = sql + " AND subs_code =  '" + migrateRuleResult.getSubsCode() + "'";
                }
                if (migrateRuleResult.getCheckRuleName() != null) {
                    sql = sql + " AND check_rule_name =  '" + migrateRuleResult.getCheckRuleName() + "'";
                }
                if (migrateRuleResult.getTableId() != null) {
                    sql = sql + " AND table_id =  '" + migrateRuleResult.getTableId() + "'";
                }
                if (migrateRuleResult.getTableCode() != null) {
                    sql = sql + " AND table_code =  '" + migrateRuleResult.getTableCode() + "'";
                }
                if (migrateRuleResult.getTableName() != null) {
                    sql = sql + " AND table_name =  '" + migrateRuleResult.getTableName() + "'";
                }
                if (migrateRuleResult.getCheckRuleSql() != null) {
                    sql = sql + " AND check_rule_sql =  '" + migrateRuleResult.getCheckRuleSql() + "'";
                }
                if (migrateRuleResult.getCheckExpectValue() != null) {
                    sql = sql + " AND check_expect_value =  '" + migrateRuleResult.getCheckExpectValue() + "'";
                }
                if (migrateRuleResult.getCheckResultValue() != null) {
                    sql = sql + " AND check_result_value =  '" + migrateRuleResult.getCheckResultValue() + "'";
                }
                if (migrateRuleResult.getCheckExpectFlag() != null) {
                    sql = sql + " AND check_expect_flag =  '" + migrateRuleResult.getCheckExpectFlag() + "'";
                }
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
				MigrateRuleResult obj = new MigrateRuleResult();
				obj.setCheckResultId(rs.getString("check_result_id"));
				obj.setCheckRuleId(rs.getString("check_rule_id"));
				obj.setSubsCode(rs.getString("subs_code"));
				obj.setCheckRuleName(rs.getString("check_rule_name"));
				obj.setTableId(rs.getString("table_id"));
				obj.setTableCode(rs.getString("table_code"));
				obj.setTableName(rs.getString("table_name"));
				obj.setCheckRuleSql(rs.getString("check_rule_sql"));
				obj.setCheckExpectValue(rs.getBigDecimal("check_expect_value"));
				obj.setCheckResultValue(rs.getBigDecimal("check_result_value"));
				obj.setCheckExpectFlag(rs.getString("check_expect_flag"));
                result.add(obj);
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateRuleResult is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
    protected void close(ResultSet theRs, Statement theStmt, PreparedStatement thePs) {
        try {
            if (theRs != null)
                theRs.close();
            if (theStmt != null)
                theStmt.close();
            if (thePs != null)
                thePs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
